package com.zyb.eduservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.R;
import com.zyb.eduservice.client.VideoClient;
import com.zyb.eduservice.entity.EduCourse;
import com.zyb.eduservice.entity.EduTeacher;
import com.zyb.eduservice.entity.vo.CourseInfoVo;
import com.zyb.eduservice.entity.vo.CoursePublishVo;
import com.zyb.eduservice.entity.vo.CourseQuery;
import com.zyb.eduservice.entity.vo.TeacherQuery;
import com.zyb.eduservice.service.EduCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Resource
    private EduCourseService courseService;

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据ID查询课程
    @GetMapping("getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable("courseId") String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程基本信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //表连接查询所有信息
    @GetMapping("publishCourseInfo/{courseId}")
    public R publishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo allCourseInfo = courseService.getAllCourseInfo(courseId);
        return R.ok().data("allCourseInfo", allCourseInfo);
    }

    //课程最终发布
    @PostMapping("releaseCourse/{courseId}")
    public R releaseCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //查询所有
    @GetMapping("findAll")
    public R findAll() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("items", list);
    }

    //分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current, @PathVariable("limit") Long limit) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        courseService.page(coursePage, null);
        Long total = coursePage.getTotal();
        List<EduCourse> rows = coursePage.getRecords();
        return R.ok().data("total", total).data("rows", rows);
    }

    //条件查询带分页
    @PostMapping("pageCourseCondition/{current}/{limit}")
    //使用RequestBody必须使用post提交
    public R pageCourseCondition(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        IPage<EduCourse> iPage = courseService.pageCourseCondition(current, limit, courseQuery);

        List<EduCourse> rows = iPage.getRecords();
        long total = iPage.getTotal();

        return R.ok().data("rows", rows).data("total", total);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R removeCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

